package com.elink.enginerring.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.elink.enginerring.co.BaseCo;
import com.elink.enginerring.co.PostbarCo;
import com.elink.enginerring.common.Constant;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.domain.Postbar;
import com.elink.enginerring.service.PostbarService;
import com.elink.enginerring.util.JSONUtil;

@RestController
@RequestMapping("api/bostbar/")
@Api(value = "百问百答列表接口")
public class PostbarController {
	@Autowired
	private PostbarService postbarService;

	/**
	 * 获取列表
	 * @param co
	 * @return
	 */
	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	@ApiOperation(value = "百问百答列表",httpMethod="GET", notes = "根据条件查找出对应的百问百答")
	public Map<String, Object> getList(@ModelAttribute @Valid PostbarCo co) {
		return postbarService.getList(co);
	}
	
	/**
	 * 根据搜索关键词获取列表
	 * @param co
	 * @return
	 */
	@RequestMapping(value = "/getListByKeyWord", method = RequestMethod.GET)
	@ApiOperation(value = "根据搜索关键词获取列表",httpMethod="GET", notes = "根据关键词找出对应的百问百答")
	public Map<String, Object> getListByKeyWord(@ModelAttribute @Valid BaseCo co,
			@ApiParam(value = "人气", required = false, defaultValue = "false")@RequestParam(value="replyCount",required = false, defaultValue = "false")Boolean replyCount,
			@ApiParam(value = "上传时间先后排序", required = false, defaultValue = "false")@RequestParam(value="time",required = false, defaultValue = "false")Boolean time,
			@ApiParam(value = "搜索关键词", required = false, defaultValue = "")@RequestParam(value="keyWord",required = false, defaultValue = "")String keyWord) {
		return postbarService.getListByKeyWord(co,replyCount,time,keyWord);
	}
	
	/**
	 * 个人中心-首页=百问百答
	 * @return
	 */
	@RequestMapping(value = "/getPostbarList", method = RequestMethod.GET)
	@ApiOperation(value = "个人中心-首页=百问百答",httpMethod="GET", notes = "个人中心-首页=百问百答")
	public Map<String, Object>getPostbarList(@ApiParam(value="国内工程二级分类id",required=true)@RequestParam(value="scId",required=true)int scId,
			@ApiParam(value="职业规划体系id",required=true)@RequestParam(value="hierId",required=true) int hierId){
		return postbarService.getPostbarList(scId,hierId);
	}
	
	/**
	 * 项目首页--百问百答
	 * @return
	 */
	@RequestMapping(value = "/getPostbarOfIndex", method = RequestMethod.GET)
	@ApiOperation(value = "项目首页--百问百答",httpMethod="GET", notes = "项目首页--百问百答")
	public Map<String, Object>getPostbarOfIndex(@ApiParam(value="返回数据的条数，默认为10",required=true,defaultValue="10")
	@RequestParam(value="count",required=true,defaultValue="10") int count){
		return postbarService.getPostbarOfIndex(count);
	}
	/**
	 * 获取详情
	 * @param pbId
	 * @return
	 */
	@RequestMapping(value = "/getInfo", method = RequestMethod.GET)
	@ApiOperation(value = "百问百答详情", httpMethod="GET", notes = "根据问题主键查找出对应的回复")
	public Map<String, Object> getInfo(@ApiParam(value = "百问百答主键id", required = true) 
	@RequestParam(value = "pbId", required = true) String pbId,@ModelAttribute @Valid BaseCo co,HttpServletRequest request) {
		String userId =null;
		try {
			@SuppressWarnings("unchecked")
			Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
			userId =  userInfo.get("userId").toString();
		} catch (Exception e) {
		}
		return postbarService.getInfo(pbId,co,userId);
	}
	
	/**
	 * 添加提问
	 * @param postbar
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ApiOperation(value = "我要提问",httpMethod="POST", notes = "增加一个提问")
	public Map<String, Object>add(@ModelAttribute @Valid Postbar postbar,
			@ApiParam(value="详情分类ids,用逗号进行分割",required=false)@RequestParam(value="dcId",required=false)String dcId,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if(userInfo != null && userInfo.containsKey("userId")){
			if((int)userInfo.get("userType")==Constant.personUserType){//只有个人用户才能够发表百问百答
				String userId =  userInfo.get("userId").toString();
				postbar.setUserId(userId);
				return postbarService.add(postbar,dcId);
			}else{
				return JSONUtil.getJSON(ErrorType.AUTHORITY_ERROR);
			}
		}
		return JSONUtil.getJSON(ErrorType.AUTHORITY_ERROR); 
	}
	
	/**
	 * 修改提问
	 * @param postbar
	 * @param dcId
	 * @param oldDcId
	 * @return
	 */
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	@ApiOperation(value = "修改提问",httpMethod="POST", notes = "修改我的提问")
	public Map<String, Object>modify(@ModelAttribute @Valid Postbar postbar,
			@ApiParam(value="详情分类ids,用逗号进行分割",required=false)@RequestParam(value="dcId",required=false)String dcId,
			@ApiParam(value="旧的详情分类ids,用逗号进行分割",required=false)@RequestParam(value="oldDcId",required=false)String oldDcId,
			HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if(userInfo != null && userInfo.containsKey("userId")){
			if((int)userInfo.get("userType")==Constant.personUserType){//只有个人用户才能够修改百问百答
				String userId =  userInfo.get("userId").toString();
				postbar.setUserId(userId);
				return postbarService.modify(postbar,dcId,oldDcId);
			}else{
				return JSONUtil.getJSON(ErrorType.AUTHORITY_ERROR);
			}
		}
		return JSONUtil.getJSON(ErrorType.AUTHORITY_ERROR); 
	}
	
	/**
	 * 获取我提问的
	 * @return
	 */
	@RequestMapping(value = "/getPostbarById", method = RequestMethod.GET)
	@ApiOperation(value = "获取我提问的", httpMethod="GET", notes = "分页获取我提问的")
	public Map<String, Object>getPostbarById(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		System.out.println("用户"+userId);
		return postbarService.getPostbarById(co,userId);
	}
	
	/**
	 * 获取TA提问的
	 * @return
	 */
	@RequestMapping(value = "/getPostbarByUserId", method = RequestMethod.GET)
	@ApiOperation(value = "获取TA提问的", httpMethod="GET", notes = "分页获取TA提问的")
	public Map<String, Object>getPostbarByUserId(@ModelAttribute @Valid BaseCo co,
			@ApiParam(value="用户ID",required=true)@RequestParam(value="userId",required=true)String userId){
		return postbarService.getPostbarById(co,userId);
	}
	
	/**
	 * 获取我的回答
	 * @param co
	 * @return
	 */
	@RequestMapping(value = "/getMyReply", method = RequestMethod.GET)
	@ApiOperation(value = "获取我的回答", httpMethod="GET", notes = "分页获取我的回答")
	public Map<String, Object>getMyReply(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		return postbarService.getReplyByUserId(co,userId);
	}
	
	/**
	 * 获取TA的回答
	 * @param co
	 * @return
	 */
	@RequestMapping(value = "/getReplyByUserId", method = RequestMethod.GET)
	@ApiOperation(value = "获取TA的回答", httpMethod="GET", notes = "分页获取TA的回答")
	public Map<String, Object>getReplyByUserId(@ModelAttribute @Valid BaseCo co,
			@ApiParam(value="用户ID",required=true)@RequestParam(value="userId",required=true)String userId){
		return postbarService.getReplyByUserId(co,userId);
	}
	
	/**
	 * 获取热门问答
	 * @return
	 */
	@RequestMapping(value = "/getHotPostbar", method = RequestMethod.GET)
	@ApiOperation(value = "获取热门问答",httpMethod="GET", notes = "获取热门问答")
	public Map<String, Object>getHotPostbar(@ApiParam(value="页面显示多少条数据,默认是10条",required=true,defaultValue="10")
	@RequestParam(value="count",required=true,defaultValue="10")int count){
		return postbarService.getHotPostbar(count);
	}
	
	
	/**
	 * 获取相关问答
	 * @param pbId
	 * @param count
	 * @return
	 */
	@RequestMapping(value = "/getRelevant", method = RequestMethod.GET)
	@ApiOperation(value = "获取相关问答",httpMethod="GET", notes = "获取相关问答")
	public Map<String, Object>getRelevant(
			@ApiParam(value = "百问百答主键id", required = true) 
			@RequestParam(value = "pbId", required = true) String pbId,
			@ApiParam(value="页面显示多少条数据,默认是10条",required=true,defaultValue="10")
	@RequestParam(value="count",required=true,defaultValue="10")int count){
		return postbarService.getRelevant(pbId,count);
	}
	
	/**
	 * 获取问答排行
	 * @param pbId
	 * @param count
	 * @return
	 */
	@RequestMapping(value = "/getPostbarRank", method = RequestMethod.GET)
	@ApiOperation(value = "获取问答排行",httpMethod="GET", notes = "搜索页面获取问答排行")
	public Map<String, Object>getPostbarRank(
			@ApiParam(value="页面显示多少条数据,默认是10条",required=true,defaultValue="10")
	@RequestParam(value="count",required=true,defaultValue="10")int count){
		return postbarService.getPostbarRank(count);
	}
	/**
	 * 关闭问题
	 * @param postbarId
	 * @return
	 */
	@RequestMapping(value = "/close", method = RequestMethod.GET)
	@ApiOperation(value = "关闭问题",httpMethod="GET", notes = "关闭问题")
	public Map<String, Object>close(@RequestParam(value = "postbarId",required=true)String postbarId,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		return postbarService.updatePostbar(postbarId,userId);
	}
}
